package org.linlinjava.litemall.admin.web;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.shiro.AuthUtils;
import org.linlinjava.litemall.core.util.AmountUtils;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallStore;
import org.linlinjava.litemall.db.domain.LitemallStoreVo;
import org.linlinjava.litemall.db.service.LitemallStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.linlinjava.litemall.admin.service.LogHelper;
import javax.validation.constraints.NotNull;
import java.util.List;

import static org.linlinjava.litemall.admin.util.AdminResponseCode.STORE_BALANCE_ERROR;
import static org.linlinjava.litemall.admin.util.AdminResponseCode.STORE_NAME_EXIST;

/**
 * @Description:	商家管理
 * @Author:			Coach tam
 * @Company:		坚持灵活  灵活坚持
 * @CreateDate:		2021-4-7 20:45:45
 */
@Slf4j
@RestController
@RequestMapping("/admin/store")
@Validated
public class AdminStoreController {
    private final Log logger = LogFactory.getLog(AdminStoreController.class);

    @Autowired
    private LitemallStoreService storeService;

    @Autowired
    private LogHelper logHelper;

    @RequiresPermissions("admin:store:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "商家管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallStore> litemallStoreList = storeService.querySelective(name, page, limit, sort, order);
        return ResponseUtil.okList(litemallStoreList);
    }
    @RequiresPermissions("admin:store:read")
    @RequiresPermissionsDesc(menu = {"商场管理", "商家管理"}, button = "详情")
    @GetMapping("/detail")
    public Object litemallStoreDetail(@NotNull Integer id) {
    	LitemallStore litemallStore=storeService.findById(id);
        return ResponseUtil.ok(litemallStore);
    }
    @RequiresPermissions("admin:store:update")
    @RequiresPermissionsDesc(menu = {"商场管理", "商家管理"}, button = "编辑")
    @PostMapping("/update")
    public Object litemallStoreUpdate(@RequestBody LitemallStoreVo litemallStore) {
        return ResponseUtil.ok(storeService.updateById(litemallStore));
    }

    @RequiresPermissions("admin:store:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "商家管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody LitemallStoreVo entity) {
        Object error = validate(entity);
        if (error != null) {
            return error;
        }

        String name = entity.getName();
        List<LitemallStore> list = storeService.findStore(name);
        if (list.size() > 0) {
            return ResponseUtil.fail(STORE_NAME_EXIST, "已经存在");
        }


        storeService.add(entity);
        logHelper.logAuthSucceed("添加商家管理", name);
        return ResponseUtil.ok(entity);
    }

    private Object validate(LitemallStore entity) {
        String name = entity.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }

        return null;
    }


    @RequiresPermissions("admin:store:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "商家管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody LitemallStore entity) {
        Integer id = entity.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }

        storeService.deleteById(id);
        logHelper.logAuthSucceed("删除商家管理", entity.getName());
        return ResponseUtil.ok();
    }
    @GetMapping("balance")
    public Object balance() {
        Integer storeId = AuthUtils.getStoreId();
        Long balance = storeService.queryBalance(storeId);
        String result = "";
        try {
            result = AmountUtils.changeF2Y(balance);
        } catch (Exception e) {
            log.error("商家余额错误", e);
            return ResponseUtil.fail(STORE_BALANCE_ERROR, "商家余额错误");
        }
        return ResponseUtil.ok(result);
    }
}
